﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.Util;
using System.Security.Claims;

namespace DFGK_WMS.WebAPI.Controllers.基础数据
{
    /// <summary>
    /// 字典类型
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TDictTypeController : ControllerBase
    {
        private readonly ILogRepository logRepository;
        private readonly ITDictTypeRepository dicr;

        public TDictTypeController(ITDictTypeRepository tDictTypeRepository, ILogRepository logRepository)
        {
            dicr = tDictTypeRepository;
            this.logRepository = logRepository;
        }
        /// <summary>
        /// 添加字典类型
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddTDictype(TDictType dic)
        {
            var res = dicr.AddTDictType(dic);
            ApiResult result = new ApiResult();
            if (res)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 4;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "创建了字典类型" + dic.TypeName;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                result.code = "200";
                result.message = "添加成功！";
                result.data = true;
                return Ok(result);
            }
            result.code = "500";
            result.message = "添加失败！编码已存在！";
            result.data = true;
            return Ok(result);
        }
        /// <summary>
        /// 查询字典类型
        /// </summary>
        /// <param name="keyword">关键字</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAllTDictType(string? keyword, int pageIndex = 1, int pageSize = 5)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            var res = dicr.GetAllTDictType(keyword, pageIndex, pageSize, out var totalCount);
            ApiResult result = new ApiResult();
            result.code = "200";
            result.data = res;
            result.count = totalCount;
            return Ok(result);
        }
        /// <summary>
        /// 是否查询树状字典类型
        /// </summary>
        /// <param name="keyword">关键字</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAllTDictTypeTree(string? keyword, int isTree = 0)
        {
            bool isTreeBool = true;
            if (isTree == 0)
                isTreeBool = false;
            else
                isTreeBool = true;
            var res = dicr.GetAllTDictTypeTree(keyword, isTreeBool);
            ApiResult result = new ApiResult();
            result.code = "200";
            result.data = res;
            return Ok(result);
        }
        /// <summary>
        /// 根据id查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTDictTypeByID(int id)
        {
            if(id<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var res = dicr.GetTDictTypeByID(id);
            ApiResult result = new ApiResult();
            result.code = "200";
            result.data = res;
            return Ok(result);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dictType"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdTDicType(TDictType dictType)
        {
            if (dictType.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            if (string.IsNullOrEmpty(dictType.TypeCode))
            {
                return Ok(BaseResult.Result("400", "必填项不能为空！"));
            }
            if (dictType.TypeName == "产品状态")
            {
                return Ok(BaseResult.Result("400", "产品状态为系统状态不可修改"));
            }
            if (dictType.TypeName == "收货单据类型")
            {
                return Ok(BaseResult.Result("400", "收货单据类型为系统状态不可修改"));
            }
            var res = dicr.UpdTDicType(dictType);
            ApiResult result = new ApiResult();
            if (res)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 6;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "修改了字典类型" + dictType.TypeName;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                result.code = "200";
                result.message = "修改成功！";
                result.data = true;
                return Ok(result);
            }
            result.code = "500";
            result.message = "修改失败！编码已存在！";
            result.data = true;
            return Ok(result);
        }
        /// <summary>
        /// 删除字典
        /// </summary>
        /// <param name="id">字典id</param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelTDicType(int id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var dic=dicr.GetTDictTypeByID(id);
            if (dic != null && dic.TypeName == "产品状态")
            {
                return Ok(BaseResult.Result("400", "产品状态为系统状态不可删除"));
            }
            if (dic != null && dic.TypeName == "收货单据类型")
            {
                return Ok(BaseResult.Result("400", "收货单据类型系统状态不可删除"));
            }
            var res = dicr.DelTDicType(id);
            ApiResult result = new ApiResult();
            if (res)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int rid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 5;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = rid;
                logg.Description = uname + "删除了字典类型";
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                result.code = "200";
                result.message = "删除成功！";
                result.data = true;
                return Ok(result);
            }
            result.code = "500";
            result.message = "删除失败！";
            result.data = true;
            return Ok(result);
        }
        /// <summary>
        /// 批量删除字典项
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelTDicTypeList(int[] idList)
        {
            if (idList.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool daRes = true;
            ApiResult result = new ApiResult();
            foreach (var id in idList)
            {
                var dic = dicr.GetTDictTypeByID(id);
                if (dic != null && dic.TypeName == "产品状态")
                {
                    return Ok(BaseResult.Result("500", "产品状态为系统状态不可删除"));
                }
                var res = dicr.DelTDicType(id);
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int rid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 5;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = rid;
                logg.Description = res ? uname + "删除了字典类型" : uname+"删除字典类型失败";
                //获取前端IP地址
                logg.Ipaddress = ip;
                logRepository.AddTLod(logg);
                if (!res)
                {
                    daRes=false;
                    result.code = "500";
                    result.message = "删除失败！";
                    result.data = true;
                }
            }
            if (daRes)
            {
                result.code = "200";
                result.message = "删除成功！";
                result.data = true;
                return Ok(result);
            }
            return Ok(result);
        }
    }
}
